package 
{
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	
	/**
	 * *
	 * 请求类：提供了向服务器发送请求的api
	 * */
	
	public class EZRequest
	{
		private var requesturl:String="";
		private var userid:String=null;
		private var requestobj:URLRequest=null;
		/**
		 * 构造函数，创建了一个Request对象
		 * @param requesturl 发送请求的URL
		 * @example 例子如下：
		 * <listing version="3.0">
		 * var requesturl:String="http://172.18.68.81:8888/POService/ActionServlet";
		 * var obj:Request=new Request(requesturl);
 		 * </listing>
		 */	
		public function EZRequest(requesturl:String)
		{
			this.requesturl=requesturl;
			createRequestUrl();
		}
		/**
		 * 设置用户ID
		 * @param userid 用户ID
		 * @example 例子如下：
		 * <listing version="3.0">
		 * var requesturl:String="http://172.18.68.81:8888/POService/ActionServlet";
		 * var obj:Request=new Request(requesturl);
		 * var userid:String="userid";
		 * obj.setUserid(userid);
 		 * </listing>
		 */
		public function setUserid(userid:String):void{
			this.userid=userid;
		}
		/**
		 * 获取当前用户ID
		 * @example 例子如下：
		 * <listing version="3.0">
		 * var requesturl:String="http://172.18.68.81:8888/POService/ActionServlet";
		 * var obj:Request=new Request(requesturl);
		 * var userid:String=obj.getUserid();
 		 * </listing>
		 */
		public function getUserid():String{
			return this.userid;
		}
		/**
		 * 获取URLRequest对象
		 * @example 例子如下：
		 * <listing version="3.0">
		 * var requesturl:String="http://172.18.68.81:8888/POService/ActionServlet";
		 * var obj:Request=new Request(requesturl);
		 * var requestobj:URLRequest=obj.getRequestObj();
 		 * </listing>
		 */
		public function getRequestObj():URLRequest{
			return this.requestobj;
		}
		/**
		 * 创建URLRequest对象
		 * @example 例子如下：
		 * <listing version="3.0">
		 * var requesturl:String="http://172.18.68.81:8888/POService/ActionServlet";
		 * var obj:Request=new Request(requesturl);
		 * obj.createRequestUrl();
 		 * </listing>
		 */
		public function createRequestUrl():void{
			this.requestobj=new URLRequest(this.requesturl);
		}
		/**
		 * 发送请求
		 * @param opid 操作ID
		 * @param urlparams 参数
		 * @param funback 函数回调方法
		 * @example 例子如下：
		 * <listing version="3.0">
		 * var requesturl:String="http://172.18.68.81:8888/POService/ActionServlet";
		 * var obj:Request=new Request(requesturl);
		 * var opid:String="opid";
		 * var urlparams:String="urlparams";
		 * var funback:Function=new Function();
		 * obj.senurl(opid,urlparams,funback);
 		 * </listing>
		 */
		public function sendurl(opid:String,urlparams:String,funback:Function):void{
			CursorManager.setBusyCursor();
			this.requestobj.method = URLRequestMethod.POST;
			
			var params:URLVariables = new URLVariables();
			var jsonstr:String='{opid:"'+opid+'",params:'+urlparams+'}';
			trace("发送："+jsonstr);
			trace(this.requesturl);
			params.jsonstr =escape(jsonstr);
			params.urltime=new Date().time;
			this.requestobj.data = params;
			var l:URLLoader = new URLLoader();  
			l.load(this.requestobj);  
			l.addEventListener(Event.COMPLETE,function(event:Event):void{
				CursorManager.removeAllCursors();
				var str : String = ((event.target) as URLLoader).data;
					trace(unescape(str));
					var result:String=unescape(str);
					var resObj:Object=JSON.parse(result);
					if(resObj.success==true){
						funback(resObj.resultJson);
					}else{
						Alert.show(resObj.resultJson,"异常信息提示");
					}
			}); 
		}
		
	}
}